package com.unbox.generate.util;

import com.unbox.generate.entity.GenerateConfig;
import com.unbox.generate.entity.GenerateInfo;
import com.unbox.generate.entity.TableInfo;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.*;

/**
 * 此部分需要开发自己定义类型的转换
 */
@Component
public class TableMapperUtil {

    @Autowired
    StringUtil stringUtil;

    @Autowired
    TableMapperUtil dtoUtil;

    /**
     * 数据库字段类型映射
     */
    HashMap<String, String> typesMap = new HashMap() {
        {
            put("varchar", "String");
            put("text", "String");
            put("int", "Integer");
            put("float", "Float");
            put("datetime", "LocalDateTime");
            put("timestamp", "LocalDateTime");
            put("bigint", "Long");
            put("tinyint", "Boolean");
            put("decimal", "BigDecimal");
        }
    };

    /**
     * 需要引入的包名
     */
    static HashMap<String, String>  typesPkgMap = new HashMap() {
        {
            put("decimal", "java.math.BigDecimal");
        }
    };

    /**
     * 获取对应的生成类型
     * @param type
     * @return
     */
    public DbType dbType(String type) {
        DbType dbType = new DbType();
        dbType.setName(typesMap.get(type));
        dbType.setPkg(typesPkgMap.get(type));
        return dbType;
    }

    @Data
    public class DbType{
        String name;
        String pkg;
    }

    /**
     *
     * @param generateConfig
     * @return
     */
    public Map merge(GenerateConfig generateConfig) {
        Map dataModel = new HashMap<>();
        dataModel.put("rootPkg", generateConfig.getRootPkg());
        dataModel.put("feignServiceName", generateConfig.getFeignServiceName());
        dataModel.put("bClassName", stringUtil.getClassName(generateConfig.getTableName().replace(generateConfig.getPrefix(), "")));
        dataModel.put("sClassName", stringUtil.toLowerCaseFirstOne(dataModel.get("bClassName").toString()));
        dataModel.put("tableComment", generateConfig.getTableComment());
        dataModel.put("tableName", generateConfig.getTableName());
        dataModel.put("visitRoot", generateConfig.getVisitRoot());
        dataModel.put("allbClassName", dataModel.get("bClassName").toString().toUpperCase());
        dataModel.put("gateWayRoot", generateConfig.getGateWayRoot());
        dataModel.put("apiVersion", generateConfig.getApiVersion());
        dataModel.put("generateType", generateConfig.getGenerateType());

        if (StringUtils.isEmpty(generateConfig.getIsSelectStr())) {
            generateConfig.setIsSelectStr("");
        }
        dataModel.put("isSelectStr", generateConfig.getIsSelectStr());
        if (StringUtils.isEmpty(generateConfig.getIsUpdateStr())) {
            generateConfig.setIsUpdateStr("");
        }
        dataModel.put("isUpdateStr", generateConfig.getIsUpdateStr());
        if (StringUtils.isEmpty(generateConfig.getIsDeleteStr())) {
            generateConfig.setIsDeleteStr("");
        }
        dataModel.put("isDeleteStr", generateConfig.getIsDeleteStr());
        if (StringUtils.isEmpty(generateConfig.getIsCreateStr())) {
            generateConfig.setIsCreateStr("");
        }
        dataModel.put("isCreateStr", generateConfig.getIsCreateStr());

        // 获取前端组件生成公共组件部分
        List<String> publicControl = new ArrayList<>();
        List<TableInfo> tableInfos = new ArrayList<>();
        String imageView = "imageView";
        String pri = "PRI";
        generateConfig.getGenerateInfos()
                .stream()
                .sorted(Comparator.comparing(GenerateInfo::getWebSortTableDisPlayNum))
                .forEach(generateInfo -> {
                    if (!StringUtils.isEmpty(generateInfo.getWebTableControl()) && generateInfo.getWebTableControl().equals(imageView)) {
                        if (!publicControl.contains(generateInfo.getWebTableControl())) {
                            publicControl.add(generateInfo.getWebTableControl());
                        }
                    }
                    TableInfo tableInfo = new TableInfo();
                    BeanUtils.copyProperties(generateInfo, tableInfo);
                    tableInfo.setJavaEntityFieldName(stringUtil.getFieldName(generateInfo.getColumnName()));
                    tableInfo.setJavaEntityBFieldName(stringUtil.toUpperCaseFirstOne(stringUtil.getFieldName(generateInfo.getColumnName())));
                    DbType dbType = dtoUtil.dbType(generateInfo.getDataType());
                    tableInfo.setJavaType(dbType.getName());
                    tableInfo.setJavaImportPkg(dbType.getPkg());
                    tableInfo.setJavaEntityFieldGetName("get"+stringUtil.toUpperCaseFirstOne(tableInfo.getJavaEntityFieldName()));
                    tableInfo.setJavaEntityFieldSetName("set"+stringUtil.toUpperCaseFirstOne(tableInfo.getJavaEntityFieldName()));
                    if (tableInfo.getJavaImportPkg() == null) {
                        tableInfo.setJavaImportPkg("");
                    }
                    if (generateInfo.getColumnKey().equals(pri)) {
                        tableInfo.setJavaIsPri("1");
                    } else {
                        tableInfo.setJavaIsPri("0");
                    }
                    tableInfos.add(tableInfo);
                });
        dataModel.put("fields", tableInfos);
        dataModel.put("publicControl", publicControl);
        return dataModel;
    }
}
